# CLEANING AND FACTOR ANALYSIS RESPECTABILITY POLITICS
# ALAN YAN
# SEPTEMBER 1, 2020

#### SETUP ####
#clear environment
rm(list = ls())

#load libraries
library(pacman)
p_load(tidyverse,
       psych)

#load data
dt <- read.csv("01-data/raw-data.csv", header = TRUE, stringsAsFactors = FALSE)

#### CLEANING ####
dt %>%
  mutate(
    rps_avoidstereobehav = case_when(
      rps_avoidstereobehav == "Strongly agree" ~ 7,
      rps_avoidstereobehav == "Agree" ~ 6,
      rps_avoidstereobehav == "Somewhat agree" ~ 5,
      rps_avoidstereobehav == "Neither agree nor disagree" ~ 4,
      rps_avoidstereobehav == "Somewhat disagree" ~ 3,
      rps_avoidstereobehav == "Disagree" ~ 2,
      rps_avoidstereobehav == "Strongly disagree" ~ 1
    ),
    rps_nomatterappropri = case_when(
      rps_nomatterappropri == "Strongly agree" ~ 7,
      rps_nomatterappropri == "Agree" ~ 6,
      rps_nomatterappropri == "Somewhat agree" ~ 5,
      rps_nomatterappropri == "Neither agree nor disagree" ~ 4,
      rps_nomatterappropri == "Somewhat disagree" ~ 3,
      rps_nomatterappropri == "Disagree" ~ 2,
      rps_nomatterappropri == "Strongly disagree" ~ 1
    ),
    rps_carriedbetterwhi = case_when(
      rps_carriedbetterwhi == "Strongly agree" ~ 7,
      rps_carriedbetterwhi == "Agree" ~ 6,
      rps_carriedbetterwhi == "Somewhat agree" ~ 5,
      rps_carriedbetterwhi == "Neither agree nor disagree" ~ 4,
      rps_carriedbetterwhi == "Somewhat disagree" ~ 3,
      rps_carriedbetterwhi == "Disagree" ~ 2,
      rps_carriedbetterwhi == "Strongly disagree" ~ 1
    ),
    rps_behavehoweverthe = case_when(
      rps_behavehoweverthe == "Strongly agree" ~ 7,
      rps_behavehoweverthe == "Agree" ~ 6,
      rps_behavehoweverthe == "Somewhat agree" ~ 5,
      rps_behavehoweverthe == "Neither agree nor disagree" ~ 4,
      rps_behavehoweverthe == "Somewhat disagree" ~ 3,
      rps_behavehoweverthe == "Disagree" ~ 2,
      rps_behavehoweverthe == "Strongly disagree" ~ 1
    ),
    rps_fewerproblems = case_when(
      rps_fewerproblems == "Strongly agree" ~ 7,
      rps_fewerproblems == "Agree" ~ 6,
      rps_fewerproblems == "Somewhat agree" ~ 5,
      rps_fewerproblems == "Neither agree nor disagree" ~ 4,
      rps_fewerproblems == "Somewhat disagree" ~ 3,
      rps_fewerproblems == "Disagree" ~ 2,
      rps_fewerproblems == "Strongly disagree" ~ 1
    ),
    rps_dontworry = case_when(
      rps_dontworry == "Strongly agree" ~ 7,
      rps_dontworry == "Agree" ~ 6,
      rps_dontworry == "Somewhat agree" ~ 5,
      rps_dontworry == "Neither agree nor disagree" ~ 4,
      rps_dontworry == "Somewhat disagree" ~ 3,
      rps_dontworry == "Disagree" ~ 2,
      rps_dontworry == "Strongly disagree" ~ 1
    ),
    rps_farebetter = case_when(
      rps_farebetter == "Strongly agree" ~ 7,
      rps_farebetter == "Agree" ~ 6,
      rps_farebetter == "Somewhat agree" ~ 5,
      rps_farebetter == "Neither agree nor disagree" ~ 4,
      rps_farebetter == "Somewhat disagree" ~ 3,
      rps_farebetter == "Disagree" ~ 2,
      rps_farebetter == "Strongly disagree" ~ 1
    ),
    rps_dontworryaboutot = case_when(
      rps_dontworryaboutot == "Strongly agree" ~ 7,
      rps_dontworryaboutot == "Agree" ~ 6,
      rps_dontworryaboutot == "Somewhat agree" ~ 5,
      rps_dontworryaboutot == "Neither agree nor disagree" ~ 4,
      rps_dontworryaboutot == "Somewhat disagree" ~ 3,
      rps_dontworryaboutot == "Disagree" ~ 2,
      rps_dontworryaboutot == "Strongly disagree" ~ 1
    ),
    rps_better.habits = case_when(
      rps_better.habits == "Strongly agree" ~ 7,
      rps_better.habits == "Agree" ~ 6,
      rps_better.habits == "Somewhat agree" ~ 5,
      rps_better.habits == "Neither agree nor disagree" ~ 4,
      rps_better.habits == "Somewhat disagree" ~ 3,
      rps_better.habits == "Disagree" ~ 2,
      rps_better.habits == "Strongly disagree" ~ 1
    ),
    samplerps_1 = case_when(
      samplerps_1 == "Strongly agree" ~ 7,
      samplerps_1 == "Agree" ~ 6,
      samplerps_1 == "Somewhat agree" ~ 5,
      samplerps_1 == "Neither agree nor disagree" ~ 4,
      samplerps_1 == "Somewhat disagree" ~ 3,
      samplerps_1 == "Disagree" ~ 2,
      samplerps_1 == "Strongly disagree" ~ 1
    ),
    samplerps_2 = case_when(
      samplerps_2 == "Strongly agree" ~ 7,
      samplerps_2 == "Agree" ~ 6,
      samplerps_2 == "Somewhat agree" ~ 5,
      samplerps_2 == "Neither agree nor disagree" ~ 4,
      samplerps_2 == "Somewhat disagree" ~ 3,
      samplerps_2 == "Disagree" ~ 2,
      samplerps_2 == "Strongly disagree" ~ 1
    ),
    samplerps_3 = case_when(
      samplerps_3 == "Strongly agree" ~ 7,
      samplerps_3 == "Agree" ~ 6,
      samplerps_3 == "Somewhat agree" ~ 5,
      samplerps_3 == "Neither agree nor disagree" ~ 4,
      samplerps_3 == "Somewhat disagree" ~ 3,
      samplerps_3 == "Disagree" ~ 2,
      samplerps_3 == "Strongly disagree" ~ 1
    ),
    samplerps_4 = case_when(
      samplerps_4 == "Strongly agree" ~ 7,
      samplerps_4 == "Agree" ~ 6,
      samplerps_4 == "Somewhat agree" ~ 5,
      samplerps_4 == "Neither agree nor disagree" ~ 4,
      samplerps_4 == "Somewhat disagree" ~ 3,
      samplerps_4 == "Disagree" ~ 2,
      samplerps_4 == "Strongly disagree" ~ 1
    ),
    samplerps_5 = case_when(
      samplerps_5 == "Strongly agree" ~ 7,
      samplerps_5 == "Agree" ~ 6,
      samplerps_5 == "Somewhat agree" ~ 5,
      samplerps_5 == "Neither agree nor disagree" ~ 4,
      samplerps_5 == "Somewhat disagree" ~ 3,
      samplerps_5 == "Disagree" ~ 2,
      samplerps_5 == "Strongly disagree" ~ 1
    ),
    samplerps_6 = case_when(
      samplerps_6 == "Strongly agree" ~ 7,
      samplerps_6 == "Agree" ~ 6,
      samplerps_6 == "Somewhat agree" ~ 5,
      samplerps_6 == "Neither agree nor disagree" ~ 4,
      samplerps_6 == "Somewhat disagree" ~ 3,
      samplerps_6 == "Disagree" ~ 2,
      samplerps_6 == "Strongly disagree" ~ 1
    ),
    r_sex = factor(case_when(
      sex == "Female" ~ "Female", 
      sex == "Male" ~ "Male"
    ),
    levels = c("Male",
               "Female")
    ),
    r_age_n = (case_when(
      age == "18 - 24" ~ 2,
      age == "25 - 34" ~ 3,
      age == "35 - 44" ~ 4,
      age == "45 - 54" ~ 5,
      age == "55 - 64" ~ 6,
      age == "65 - 74" ~ 7,
      age == "75 - 84" ~ 8,
      age == "85 or older" ~ 9
    ) - 2)/7,
    r_age = factor(age,
                   levels = 
                     c("Under 18",
                       "18 - 24",
                       "25 - 34",
                       "35 - 44",
                       "45 - 54",
                       "55 - 64",
                       "65 - 74",
                       "75 - 84",
                       "85 or older")
                   ),
    r_edu_n = (case_when(
      education == "Less than high school" ~ 1,
      education == "High school graduate" ~ 2,
      education == "Some college" ~ 3,
      education == "2 year degree" ~ 4,
      education == "4 year degree" ~ 5,
      education == "Professional degree" ~ 6,
      education == "Doctorate" ~ 7
    ) - 6)/6,
    r_edu = factor(
      education,
      levels = c(
        "Less than high school",
        "High school graduate",
        "Some college",
        "2 year degree",
        "4 year degree",
        "Professional degree",
        "Doctorate"
      )
    ),
    r_income_n = (case_when(
      income == "Less than $10,000" ~ 1,
      income == "$10,000 - $19,999" ~ 2,
      income == "$20,000 - $29,999" ~ 3,
      income == "$30,000 - $39,999" ~ 4,
      income == "$40,000 - $49,999" ~ 5,
      income == "$50,000 - $59,999" ~ 6,
      income == "$60,000 - $69,999" ~ 7,
      income == "$70,000 - $79,999" ~ 8,
      income == "$80,000 - $89,999" ~ 9,
      income == "$90,000 - $99,999" ~ 10,
      income == "$100,000 - $149,999" ~ 11,
      income == "More than $150,000" ~ 12
    ) - 1)/11,
    r_income = factor(income,
                      levels = c("Less than $10,000",
                                 "$10,000 - $19,999",
                                 "$20,000 - $29,999",
                                 "$30,000 - $39,999",
                                 "$40,000 - $49,999",
                                 "$50,000 - $59,999",
                                 "$60,000 - $69,999",
                                 "$70,000 - $79,999",
                                 "$80,000 - $89,999",
                                 "$90,000 - $99,999",
                                 "$100,000 - $149,999",
                                 "More than $150,000")
                      ),
    r_region = factor(region,
                      levels = c("South",
                                 "Midwest",
                                 "Northeast",
                                 "West")),
    r_south = case_when(
      r_region == "South" ~ "South",
      r_region %in% c("Midwest",
                      "Northeast",
                      "West") ~ "Non-South"
    ),
    r_pid_n = (case_when(
      strongdem == "Strong" ~ 1, 
      strongdem == "Not very strong" ~ 2,
      pidcloser == "Closer to Democratic Party" ~ 3,
      pidcloser == "Neither" ~ 4,
      pidcloser == "Closer to Republican Party" ~ 5,
      strongrep == "Not very strong" ~ 6,
      strongrep == "Strong" ~ 7
    ) - 7)/(-6),
    r_pid = factor(
      case_when(
        strongdem == "Strong" ~ "Strong Democrat", 
        strongdem == "Not very strong" ~ "Weak Democrat",
        pidcloser == "Closer to Democratic Party" ~ "Lean Democrat",
        pidcloser == "Neither" ~ "Neither",
        pidcloser == "Closer to Republican Party" ~ "Lean Republican",
        strongrep == "Not very strong" ~ "Weak Republican",
        strongrep == "Strong" ~ "Strong Republican"
      ),
      levels = c("Strong Democrat",
                 "Weak Democrat",
                 "Lean Democrat",
                 "Neither",
                 "Lean Republican",
                 "Weak Republican",
                 "Strong Republican")
    ),
    r_ideology_n = (case_when(
      ideol == "Extremely conservative" ~ 7,
      ideol == "Conservative" ~ 6,
      ideol == "Slightly conservative" ~ 5,
      ideolchoose == "Conservative" ~ 5,
      ideolchoose == "Moderate" ~ 4,
      ideolchoose == "Liberal" ~ 3,
      ideol == "Slightly liberal" ~ 3,
      ideol == "Liberal" ~ 2,
      ideol == "Extremely liberal" ~ 1
    ) - 7)/(-6),
    r_ideology = factor(
      case_when(
      ideol == "Extremely conservative" ~ "Extremely conservative",
      ideol == "Conservative" ~ "Conservative",
      ideol == "Slightly conservative" ~ "Slightly conservative",
      ideolchoose == "Conservative" ~ "Slightly conservative",
      ideolchoose == "Moderate" ~ "Moderate",
      ideolchoose == "Liberal" ~ "Slightly liberal",
      ideol == "Slightly liberal" ~ "Slightly liberal",
      ideol == "Liberal" ~ "Liberal",
      ideol == "Extremely liberal" ~ "Extremely liberal"
    ),
    levels = c("Extremely liberal",
               "Liberal",
               "Slightly liberal",
               "Moderate",
               "Slightly conservative",
               "Conservative",
               "Extremely conservative")
    ),
    r_religious_imp_n = (case_when(
      religimpt == "Not at all important" ~ 1,
      religimpt == "Slightly important" ~ 2,
      religimpt == "Moderately important" ~ 3,
      religimpt == "Very important" ~ 4,
      religimpt == "Extremely important" ~ 5
    ) - 1)/4,
    attn_check = ifelse(AttnCheck == "Oppose a little", 1, 0),
    climate_n = (case_when(
      climate == "Not at all important" ~ 5,
      climate == "Slightly important" ~ 4,
      climate == "Moderately important" ~ 3,
      climate == "Very important" ~ 2,
      climate == "Extremely important" ~ 1
    ) - 1)/4,
    houstonschool_n = (case_when(
      houstonschool == "Extremely unreasonable" ~ 1,
      houstonschool == "Moderately unreasonable" ~ 2,
      houstonschool == "Slightly unreasonable" ~ 3,
      houstonschool == "Neither reasonable nor unreasonable" ~ 4,
      houstonschool == "Slightly reasonable" ~ 5,
      houstonschool == "Moderately reasonable" ~ 6,
      houstonschool == "Extremely reasonable" ~ 7
    ) - 1)/6,
    saggingpants_n = (case_when(
      saggingpants == "Oppose a great deal" ~ 1,
      saggingpants == "Oppose a moderate amount" ~ 2,
      saggingpants == "Oppose a little" ~ 3,
      saggingpants == "Neither favor nor oppose" ~ 4,
      saggingpants == "Favor a little" ~ 5,
      saggingpants == "Favor a moderate amount" ~ 6,
      saggingpants == "Favor a great deal" ~ 7
    ) - 1)/6,
    immigration_n = (case_when(
      immigration == "Decreased a lot" ~ 5,
      immigration == "Decreased a little" ~ 4,
      immigration == "Kept the same" ~ 3,
      immigration == "Increased a little" ~ 2,
      immigration == "Increased a lot" ~ 1
    ) - 1)/4,
    blm_n = (case_when(
      BLM == "Extremely appropriate" ~ 1,
      BLM == "Moderately appropriate" ~ 2,
      BLM == "Slightly appropriate" ~ 3,
      BLM == "Neither appropriate nor inappropriate" ~ 4,
      BLM == "Slightly inappropriate" ~ 5,
      BLM == "Moderately inappropriate" ~ 6,
      BLM == "Extremely inappropriate" ~ 7,
    ) - 1)/6,
    loitering_n = (case_when(
      loitering == "Extremely appropriate" ~ 7,
      loitering == "Moderately appropriate" ~ 6,
      loitering == "Slightly appropriate" ~ 5,
      loitering == "Neither appropriate nor inappropriate" ~ 4,
      loitering == "Slightly inappropriate" ~ 3,
      loitering == "Moderately inappropriate" ~ 2,
      loitering == "Extremely inappropriate" ~ 1,
    ) - 1)/6,
    welfarespending_n = (case_when(
      welfarespending == "Increased a lot" ~ 1,
      welfarespending == "Increased a little" ~ 2,
      welfarespending == "Kept the same" ~ 3,
      welfarespending == "Decreased a little" ~ 4,
      welfarespending == "Decreased a lot" ~ 5
    ) - 1)/4,
    threestrikes_n = (case_when(
      threestrikes == "Extremely unreasonable" ~ 1,
      threestrikes == "Moderately unreasonable" ~ 2,
      threestrikes == "Slightly unreasonable" ~ 3,
      threestrikes == "Neither reasonable nor unreasonable" ~ 4,
      threestrikes == "Slightly reasonable" ~ 5,
      threestrikes == "Moderately reasonable" ~ 6,
      threestrikes == "Extremely reasonable" ~ 7
    ) - 1)/6,
    welfarework_n = (case_when(
      welfarework == "Not at all important" ~ 1,
      welfarework == "Slightly important" ~ 2,
      welfarework == "Moderately important" ~ 3,
      welfarework == "Very important" ~ 4,
      welfarework == "Extremely important" ~ 5
    ) - 1)/4,
    justifiedshooting_n = (case_when(
      justifiedshooting == "Never" ~ 1,
      justifiedshooting == "Sometimes" ~ 2,
      justifiedshooting == "About half the time" ~ 3,
      justifiedshooting == "Most of the time" ~ 4,
      justifiedshooting == "Always" ~ 5
    ) - 1)/4,
    deathpenalty_n = (case_when(
      deathpenalty == "Oppose a great deal" ~ 1,
      deathpenalty == "Oppose a moderate amount" ~ 2,
      deathpenalty == "Oppose a little" ~ 3,
      deathpenalty == "Neither favor nor oppose" ~ 4,
      deathpenalty == "Favor a little" ~ 5,
      deathpenalty == "Favor a moderate amount" ~ 6,
      deathpenalty == "Favor a great deal" ~ 7
    ) - 1)/6,
    truancy_n = (case_when(
      truancy == "Extremely appropriate" ~ 7,
      truancy == "Moderately appropriate" ~ 6,
      truancy == "Slightly appropriate" ~ 5,
      truancy == "Neither appropriate nor inappropriate" ~ 4,
      truancy == "Slightly inappropriate" ~ 3,
      truancy == "Moderately inappropriate" ~ 2,
      truancy == "Extremely inappropriate" ~ 1,
    ) - 1)/6,
    aff.action_n = (case_when(
      aff.action == "Favor a great deal" ~ 1,
      aff.action == "Favor a moderate amount" ~ 2,
      aff.action == "Favor a little" ~ 3,
      aff.action == "Neither favor nor oppose" ~ 4,
      aff.action == "Oppose a little" ~ 5,
      aff.action == "Oppose a moderate amount" ~ 6,
      aff.action == "Oppose a great deal" ~ 7
    ) - 1)/6,
    drugcrimes_n = (case_when(
      drugcrimes == "Favor a great deal" ~ 7,
      drugcrimes == "Favor a moderate amount" ~ 6,
      drugcrimes == "Favor a little" ~ 5,
      drugcrimes == "Neither favor nor oppose" ~ 4,
      drugcrimes == "Oppose a little" ~ 3,
      drugcrimes == "Oppose a moderate amount" ~ 2,
      drugcrimes == "Oppose a great deal" ~ 1
    ) - 1)/6,
    idimpt_n = (case_when(
      idimpt == "Not at all important" ~ 1,
      idimpt == "Slightly important" ~ 2,
      idimpt == "Moderately important" ~ 3,
      idimpt == "Very important" ~ 4,
      idimpt == "Extremely important" ~ 5
    ) - 1)/4,
    linked_fate_n = (case_when(
      lfateyn == "No" ~ 1,
      lfateamt == "Not very much" ~ 2,
      lfateamt == "Some" ~ 3,
      lfateamt == "A lot" ~ 4,
    ) - 1)/3,
    discblacks_n = (case_when(
      discblacks == "None at all" ~ 1,
      discblacks == "A little" ~ 2,
      discblacks == "A moderate amount" ~ 3,
      discblacks == "A lot" ~ 4,
      discblacks == "A great deal" ~ 5
    ) - 1)/4,
    shame = (case_when(
      emotions_1 == "Never" ~ 1,
      emotions_1 == "Sometimes" ~ 2,
      emotions_1 == "About half the time" ~ 3,
      emotions_1 == "Most of the time" ~ 4,
      emotions_1 == "Always" ~ 5,
    ) - 1)/4,
    anger = (case_when(
      emotions_2 == "Never" ~ 1,
      emotions_2 == "Sometimes" ~ 2,
      emotions_2 == "About half the time" ~ 3,
      emotions_2 == "Most of the time" ~ 4,
      emotions_2 == "Always" ~ 5,
    ) - 1)/4,
    pride = (case_when(
      emotions_3 == "Never" ~ 1,
      emotions_3 == "Sometimes" ~ 2,
      emotions_3 == "About half the time" ~ 3,
      emotions_3 == "Most of the time" ~ 4,
      emotions_3 == "Always" ~ 5,
    ) - 1)/4,
    frustration = (case_when(
      emotions_4 == "Never" ~ 1,
      emotions_4 == "Sometimes" ~ 2,
      emotions_4 == "About half the time" ~ 3,
      emotions_4 == "Most of the time" ~ 4,
      emotions_4 == "Always" ~ 5,
    ) - 1)/4,
    happiness = (case_when(
      emotions_5 == "Never" ~ 1,
      emotions_5 == "Sometimes" ~ 2,
      emotions_5 == "About half the time" ~ 3,
      emotions_5 == "Most of the time" ~ 4,
      emotions_5 == "Always" ~ 5,
    ) - 1)/4,
    surprise = (case_when(
      emotions_6 == "Never" ~ 1,
      emotions_6 == "Sometimes" ~ 2,
      emotions_6 == "About half the time" ~ 3,
      emotions_6 == "Most of the time" ~ 4,
      emotions_6 == "Always" ~ 5,
    ) - 1)/4,
    closeblacks_n = (case_when(
      closeblacks == "Not at all close" ~ 1,
      closeblacks == "Slightly close" ~ 2,
      closeblacks == "Moderately close" ~ 3,
      closeblacks == "Very close" ~ 4,
      closeblacks == "Extremely close" ~ 5
    ) - 1)/4,
    introjection_n = (case_when(
      introjection == "Extremely likely" ~ 7,
      introjection == "Moderately likely" ~ 6,
      introjection == "Slightly likely" ~ 5,
      introjection == "Neither likely nor unlikely" ~ 4,
      introjection == "Slightly unlikely" ~ 3,
      introjection == "Moderately unlikely" ~ 2,
      introjection == "Extremely unlikely" ~ 1
    ) - 1)/6,
    closewhites_n = (case_when(
      closewhites == "Not at all close" ~ 1,
      closewhites == "Slightly close" ~ 2,
      closewhites == "Moderately close" ~ 3,
      closewhites == "Very close" ~ 4,
      closewhites == "Extremely close" ~ 5
    ) - 1)/4,
    closeness_diff = (closeblacks_n - closewhites_n)/2,
    closefriends_n = (case_when(
      closefriends == "None of my close friends are Black" ~ 1,
      closefriends == "Some of my close friends are Black" ~ 2,
      closefriends == "About half of my close friends are Black" ~ 3,
      closefriends == "Most of my close friends are Black" ~ 4,
      closefriends == "All of my close friends are Black" ~ 5
    ) - 1)/4,
    hardworking_n = (case_when(
      hardworking == "1\nHardworking" ~ 1,
      hardworking == "2\n " ~ 2,
      hardworking == "3\n " ~ 3,
      hardworking == "4\n " ~ 4,
      hardworking == "5\n " ~ 5,
      hardworking == "6\n " ~ 6,
      hardworking == "7\nLazy" ~ 7
    ) - 1)/6,
    intelligent_n = (case_when(
      intelligent == "1\nIntelligent" ~ 1,
      intelligent == "2\n " ~ 2,
      intelligent == "3\n " ~ 3,
      intelligent == "4\n " ~ 4,
      intelligent == "5\n " ~ 5,
      intelligent == "6\n " ~ 6,
      intelligent == "7\nUnintelligent" ~ 7
    ) - 1)/6,
    nonviolent_n = (case_when(
      nonviolent == "1\nNon-violent" ~ 1,
      nonviolent == "2\n " ~ 2,
      nonviolent == "3\n " ~ 3,
      nonviolent == "4\n " ~ 4,
      nonviolent == "5\n " ~ 5,
      nonviolent == "6\n " ~ 6,
      nonviolent == "7\nViolent" ~ 7
    ) - 1)/6,
    contributesposimage_n = (case_when(
      contributesposimage == "1\nContribute to a positive image of the group" ~ 1,
      contributesposimage == "2\n " ~ 2,
      contributesposimage == "3\n " ~ 3,
      contributesposimage == "4\n " ~ 4,
      contributesposimage == "5\n " ~ 5,
      contributesposimage == "6\n " ~ 6,
      contributesposimage == "7\nContribute to a negative image of the group" ~ 7
    ) - 1)/6,
    auth_respectelders_n = case_when(
      auth_respectelders == "Independence" ~ 0,
      auth_respectelders == "Respect for elders" ~ 1
    ),
    auth_goodmanners_n = case_when(
      auth_goodmanners == "Curiosity" ~ 0,
      auth_goodmanners == "Good manners" ~ 1
    ),
    auth_obedience_n = case_when(
      auth_obedience == "Self-reliance" ~ 0,
      auth_obedience == "Obedience" ~ 1
    ),
    auth_wellbehaved_n = case_when(
      auth_wellbehaved == "Being considerate" ~ 0,
      auth_wellbehaved == "Well-behaved" ~ 1
    ),
  ) %>%
  select(
    rps_avoidstereobehav,
    rps_nomatterappropri,
    rps_carriedbetterwhi,
    rps_behavehoweverthe,
    rps_fewerproblems,
    rps_dontworry,
    rps_farebetter,
    rps_dontworryaboutot,
    rps_better.habits,
    samplerps_1,
    samplerps_2,
    samplerps_3,
    samplerps_4,
    samplerps_5,
    samplerps_6,
    r_sex,
    r_age_n,
    r_age,
    r_edu_n,
    r_edu,
    r_income_n,
    r_income,
    r_region,
    r_south,
    r_pid_n,
    r_pid,
    r_ideology_n,
    r_ideology,
    r_religious_imp_n,
    attn_check,
    climate_n,
    houstonschool_n,
    saggingpants_n,
    immigration_n,
    blm_n,
    loitering_n,
    welfarespending_n,
    threestrikes_n,
    welfarework_n,
    justifiedshooting_n,
    deathpenalty_n,
    truancy_n,
    aff.action_n,
    drugcrimes_n,
    idimpt_n,
    linked_fate_n,
    discblacks_n,
    shame,
    anger,
    pride,
    frustration,
    happiness,
    surprise,
    closeblacks_n,
    introjection_n,
    closewhites_n,
    closefriends_n,
    closeness_diff,
    hardworking_n,
    intelligent_n,
    nonviolent_n,
    contributesposimage_n,
    auth_respectelders_n,
    auth_goodmanners_n,
    auth_obedience_n,
    auth_wellbehaved_n,
    gc
  ) %>%
  drop_na(
    rps_avoidstereobehav,
    rps_nomatterappropri,
    rps_carriedbetterwhi,
    rps_behavehoweverthe,
    rps_fewerproblems,
    rps_dontworry,
    rps_farebetter,
    rps_dontworryaboutot,
    rps_better.habits,
    samplerps_1,
    samplerps_2,
    samplerps_3,
    samplerps_4,
    samplerps_5,
    samplerps_6
  ) -> factor.data

#### FACTOR ANALYSIS ####
factor.data %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  select(
    rps_carriedbetterwhi,
    rps_fewerproblems,
    rps_farebetter,
    samplerps_4,
    samplerps_5,
    samplerps_6
  ) %>%
  factanal(., factors = 2, rotation = "promax") -> factor.output

print(factor.output, digits = 2, cutoff = .0, sort=TRUE)

#### ALPHA TESTS ####
#### *ALL RPS ITEMS ####
factor.data %>%
  select(
    rps_carriedbetterwhi,
    rps_fewerproblems,
    rps_farebetter,
    samplerps_4,
    samplerps_5,
    samplerps_6
  ) %>%
  as.matrix() %>%
  psych::alpha()

#### *IDEOLOGY RPS ITEMS ####
factor.data %>%
  select(
    rps_carriedbetterwhi,
    rps_fewerproblems,
    rps_farebetter,
  ) %>%
  as.matrix() %>%
  psych::alpha()

#### *CONCERN RPS ITEMS ####
factor.data %>%
  select(
    samplerps_4,
    samplerps_5,
    samplerps_6
  ) %>%
  as.matrix() %>%
  psych::alpha()

#### *CREATE NEW RPS INDEX VARIABLES ####
factor.data %>%
  mutate(
    rps_attribution = (rps_carriedbetterwhi + rps_fewerproblems + rps_farebetter - 3)/18,
    rps_concern = (samplerps_4 + samplerps_5 + samplerps_6 - 3)/18,
    rps_index = (rps_carriedbetterwhi + rps_fewerproblems + rps_farebetter + samplerps_4 + samplerps_5 + samplerps_6 - 6)/36,
    rps_quadrants = case_when(
      rps_attribution <= .07 & rps_concern <= .57 ~ 1,
      rps_attribution <= .07 & rps_concern >= .99 ~ 2,
      rps_attribution >= .63 & rps_concern <= .57 ~ 3,
      rps_attribution >= .63 & rps_concern >= .99 ~ 4,
    )
  ) -> factor.data

#### CREATE STEREOTYPE INDEX ####
factor.data %>%
  select(
    hardworking_n, 
    intelligent_n,
    nonviolent_n,
    contributesposimage_n
  ) %>%
  as.matrix() %>%
  alpha()

factor.data %>%
  mutate(
    stereotype = (hardworking_n + 
                    intelligent_n + 
                    nonviolent_n + 
                    contributesposimage_n)/4
         ) -> factor.data

#### CREATE AUTHORITARIAN INDEX ####
factor.data %>%
  select(
    auth_goodmanners_n,
    auth_respectelders_n,
    auth_obedience_n,
    auth_wellbehaved_n
  ) %>%
  as.matrix() %>%
  alpha()

factor.data %>%
  mutate(
    auth_index = (auth_respectelders_n + 
                    auth_goodmanners_n + 
                    auth_obedience_n +
                    auth_wellbehaved_n)/4
  ) -> factor.data

#### CREATE WEIGHTS ####
factor.data %>%
  mutate(
    weights_gender = case_when(
      r_sex == "Female" ~ .66,
      r_sex == "Male" ~ 2.27
    ),
    weights_age = case_when(
      r_age_n == 2 ~ .94,
      r_age_n == 3 ~ .99,
      r_age_n == 4 ~ 1.07,
      r_age_n == 5 ~ 1.29,
      r_age_n == 6 ~ .49,
      r_age_n == 7 ~ .32,
      r_age_n == 8 ~ 1.17,
      r_age_n == 9 ~ 1.17,
    ),
    weights_demo = weights_gender*weights_age
  ) -> factor.data

#### EXPORT DATA ####
write.csv(factor.data, "01-data/clean-data.csv")
write_rds(factor.data, "01-data/clean-data")
